package zkrt.cc.fishelevatorservice.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import zkrt.cc.fishelevatorservice.filter.UserContextHolder;
import zkrt.cc.fishelevatorservice.filter.vo.UserRightViewModel;

import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {



    @Override
    public void insertFill(MetaObject metaObject) {
        UserRightViewModel userInfo = UserContextHolder.getUserInfo();
        this.strictInsertFill(metaObject,"createUser",String.class, Objects.nonNull(userInfo)?userInfo.getUserCode():"");
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserRightViewModel userInfo = UserContextHolder.getUserInfo();
        this.strictUpdateFill(metaObject,"updateUser",String.class,Objects.nonNull(userInfo)?userInfo.getUserCode():"");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
